package com.example.recordingpen_datacollection.redisService;

import com.example.recordingpen_datacollection.Utils.DateUtils.TimeJudgeUtils;
import com.example.recordingpen_datacollection.Utils.String.StringUtils;
import com.example.recordingpen_datacollection.entity.BaiduKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class RedisService {


    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    //添加分布式锁
    public <T> Boolean setNxCacheObject(final String key, final T value,long lt,TimeUnit tu)
    {
        return redisTemplate.opsForValue().setIfAbsent(key,value,lt,tu);
    }



    /**
     * token 存储
     * @param key
     * @param timeout   时间小时
     * @param value     值
     */
    public void setTokenValueDAYS(String key, long timeout, Object value) {
        redisTemplate.opsForValue().set(key, value);
        redisTemplate.expire(key,timeout, TimeUnit.DAYS);
    }

    /**
     * token 存储 刷新
     * @param key
     * @param timeout   时间小时
     */
    public void setTokenValueDAYSReset(String key, long timeout) {
        redisTemplate.expire(key,timeout, TimeUnit.DAYS);
    }

    /**
     * token 存储
     * @param key
     * @param timeout   时间小时
     * @param value     值
     */
    public void setTokenValue(String key, long timeout, Object value) {
        redisTemplate.opsForValue().set(key, value);
        redisTemplate.expire(key,timeout, TimeUnit.HOURS);
    }

    /**
     * token 存储
     * @param key
     * @param timeout   时间秒
     * @param value     值
     */
    public void setTokenValue2(String key, long timeout, Object value) {
        redisTemplate.opsForValue().set(key, value);
        redisTemplate.expire(key,timeout, TimeUnit.SECONDS);
    }

    /**
     *  存储
     *
     *
     * @param key
     * @param value     值
     */
    public void setValue(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);

    }

    /**
     * 获取值
     * @param key
     * @return
     */
    public Object getValue(String key) {
        Object value = redisTemplate.opsForValue().get(key);
        if (value != null) {
            // 处理存在的情况
            System.out.println("Value exists: " + value);
        } else {
            // 处理不存在的情况
            System.out.println("Value does not exist");
            return null;
        }
        return value;
    }

    /**
     * 查询Redis中token的剩余时间
     * @param token
     * @return
     */
    public long getTokenDate(String token){
        Long expire =redisTemplate.getExpire(token);
        if(expire != null){
            return expire;
        } else {
            return 0;
        }
    }

    /**
     * 查询key是否存在
     * @param str
     * @return
     */

    public boolean getKeyData(String str){
        boolean delete = redisTemplate.hasKey(str);
        return delete;
    }

    /**
     * 删除 key
     * @param str
     * @return
     */
    public boolean delKeyData(String str){
        boolean delete = redisTemplate.delete(str);
        return delete;
    }

    //    根据value查key 并且删除key;

    /**
     *
     * @param value
     */
    public Long deleteKeyByValue(String value) {
        Set<String> keysToDelete = new HashSet<>();
        Set<String> keys = redisTemplate.keys("*");
        for (String key : keys) {
            Object val = redisTemplate.opsForValue().get(key);
            if (value.equals(val)) {
                keysToDelete.add(key);
            }
        }
        Long delete = redisTemplate.delete(keysToDelete);
        return delete;
    }
    // 其他操作方法...


    //修改数据一致性   百度key专业
    public synchronized  boolean ModifyingDataConsistency_BAIDUKEY(BaiduKey baiduKey)  {
        List<BaiduKey> list  = (List<BaiduKey>) getValue("SQLSERVER_BAIDU_KEY");
        System.out.println("开始");
        for (int i = 0; i < list.size(); i++) {

            if(list.get(i).getKeyid() .equals(baiduKey.getKeyid())){
                System.out.println("baiduKey.toString()->"+baiduKey.toString());
                //百度key
                if(StringUtils.isNotEmpty(baiduKey.getKeyValue())){
                    list.get(i).setKeyValue(baiduKey.getKeyValue());
                }
                //修改时间
                list.get(i).setAltertime(TimeJudgeUtils.strData(TimeJudgeUtils.getNowDate()));
                //使用次数
                if(baiduKey.getNumberOfUse()!=null){
                    list.get(i).setNumberOfUse(baiduKey.getNumberOfUse());
                }

                //过期时间
                if(baiduKey.getExpirationTime()!=null){
                    list.get(i).setExpirationTime(baiduKey.getExpirationTime());
                }
                //总次数
                if(baiduKey.getTotalCites() != null){
                    list.get(i).setTotalCites(baiduKey.getTotalCites());
                }
                if(baiduKey.getSign() != null){
                    list.get(i).setSign(baiduKey.getSign());
                }


            }
            System.out.println(list.get(i).toString());
        }
        setValue("SQLSERVER_BAIDU_KEY",list);
        return true;
    }

}